<?php

require_once("./modules/util.inc");

class AdminSettingsRenderer extends Renderer
{
	function pageLoad()
	{			
		switch( $this->action )
		{
			case "save":
				$this->saveDetails();
			case "show":
			default:
				break;
		}
	}

	function isPermitted()
	{
		if ( isset($this->user) && $this->user->type == 2 )
			return true;
		else
			return false;
	}
	
	function saveDetails()
	{
		try
		{
			$errors = Array();
			$fields = Array();
			
			$screenName = Util::safeGetPost("screenName");
			$password = Util::safeGetPost("password");
			
			if(strlen($screenName) == 0)
			{
				$fields[] = "ScreeName";
				$errors[] = "Screen name cannot be empty";
			}
			if(strlen($password) == 0)
			{
				$fields[] = "Password";
				$errors[] = "Password cannot be empty";
			}
			
			if(sizeof($errors) == 0)
			{
				$this->user->screenName = $screenName;
				if($password != '*********')
					$this->user->setPassword($password); 
				$this->user->update();
				$_SESSION["user"] = serialize($this->user);
			}
			
			$result = "{ fields:[";
			for($i =0; $i < sizeof($fields); $i++)
			{
				if( $i > 0)
					$result .= ",";
				$result .= "{ field :'".$fields[$i]."', error :'".$errors[$i]."' }";
			}
			$result .= "] }";
			
			die($result);
		}
		catch(Exception $ex)
		{
			die("{Exception:".$ex.getMessage()."}");
		}
	}
	
	function renderContent()
	{
		global $content;		
		
		$content = 
		"
		<script>
		function savePersonalSettings()
		{
			var name = $('#txtScreenName').val();
			var pwd = $('#txtPassword').val();
				
			var debug = '&debug_session_id=1000&start_debug=1&debug_start_session=1&debug_host=127.0.0.1&debug_no_cache=1241860460046&debug_port=10001&send_sess_end=1&original_url=http%3A%2F%2F127.0.0.1%3A81%2Fseositer%2Findex.php&debug_stop=1';
			var url = document.location.href;
			url = url.replace(/\?.*/ig,'?pg=admin_settings&act=save&screenName='+name+'&password='+pwd);
			$.getJSON(url,
				function(data)
				{
					
					var error = '';
					$('#lblError').children().remove();
					$('#lblError').text('');
					$.each(data.fields, function(i,field){
						error += field.error + '<br/>';
						});
						if(error == '')
							$('#lblError').append('Details saved successfully');
						else
							$('#lblError').append(error);
				});
		}
		</script>
		";
		
		$content .="
		<ul class='tabOptions'>
			<li class='tabOption tabOptionSelected'>Personal settings</li>
			<li class='tabOption'>TBD</li>
		</ul>
		<div class='tabPanel'>
			<fieldset>
				 <legend>Personal details</legend>
				 username: <input id='txtUsername' disabled='1' type='text' value='".$this->user->username."'></input><br/>
				 screen name: <input id='txtScreenName' type='text' value='".$this->user->screenName."'></input><br/>
				 password: <input id='txtPassword' type='password' value='*********' onfocus=\"this.value ='';\"></input><br/>
				 <input style='width:100px;' type='button' value='Save' onclick='javascript:savePersonalSettings()'/><br/>
				 <label id='lblError' style='color:red'></label>
			</fieldset>
		</div>";
	}

	function renderBreadcrumbs()
	{
		parent::renderBreadcrumbs();
		
		global $breadcrumbs;
		
		$breadcrumbs .= "&nbsp;&#187;&nbsp;Admin settings";
	}
	
}

$renderer = new AdminSettingsRenderer();
?>